package com.stu.covid.business.manager;

import com.stu.covid.business.constant.Constants;
import com.stu.covid.config.redis.RedisCloudHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class SystemManager {

    @Autowired
    private RedisCloudHandle redisCloudHandle;


    public String getIncreaseNumber(String prefix, String role, long delta, Integer length) {
        log.info("开始生成自增编号:{}", role);
        StringBuffer sb = new StringBuffer(prefix);
        String redisKey = role.equals("student") ? Constants.REDIS_STUDENT_NO_KEY : Constants.REDIS_TEACHER_NO_KEY;
        long redisInc = redisCloudHandle.incr(redisKey, delta);
        String redisIncString = String.valueOf(redisInc);
        if (redisIncString.length() < length) {
            int zeroNum = length - redisIncString.length();
            for (int i = 0; i < zeroNum; i++) {
                redisIncString = "0" + redisIncString;
            }
        }
        sb.append(redisIncString);
        String orderNumber = sb.toString();
        log.info("生成自增编号:{}", orderNumber);

        return orderNumber;
    }
}
